var t = require("../../../../@babel/runtime/helpers/defineProperty"), e = getApp(), a = require("../../../../mycomponent/yz_pay/yz_pay");

Component({
    behaviors: [ a ],
    properties: {
        store_id: {
            type: null
        },
        show1: {
            type: null
        },
        payMuch: {
            type: null
        }
    },
    lifetimes: {
        ready: function() {
            this.getAgreement(), this.data.show1 && this.confirmOrder();
        }
    },
    data: {
        language: "",
        agreementPayData: "",
        agreementPay: !1,
        agreementPayShow: !1,
        store_id: "",
        confirmOrderStatus: !1,
        payMuch: "0",
        realname: "",
        mobile: "",
        popupCouponSpecs: !1,
        checkCouponList: [],
        coupon_size: 0,
        use_coupon_size: 0,
        coupons: [],
        order_ids: "",
        income: {
            money_total: 0,
            sure_withdraw_money: 0
        },
        store_set: {
            is_cash_pay: "",
            is_open_cashier: "",
            is_write_information: ""
        },
        payPrice: 0,
        checkDeductionList: [],
        form: {
            mobile: "",
            realname: ""
        },
        allData: {},
        order_data: "",
        loadDiscount: !1,
        goods_price: 0,
        dispatch_price: 0,
        discount_price: 0,
        price: 0,
        deduction_price: 0,
        payType: [],
        typename: "",
        show1: !1,
        clicktag: 0,
        uid: "",
        cashier_type: "",
        hotel_id: "",
        order_pay_id: "",
        showPacket: !1,
        redirectData: {}
    },
    methods: {
        fromChild: function(t) {
            if ("0.00" == this.data.money || "0.00" == t.detail.money) this.submit("54"); else {
                var e = t.detail.submitArr;
                this.submit(e);
            }
        },
        onClickHide: function() {
            this.setData({
                showPacket: !1
            }), this.toRedirect();
        },
        toPacket: function() {
            wx.redirectTo({
                url: "/packageE/ConsumePacket/ConsumePacketSuccess/ConsumePacketSuccess?order_id=" + this.data.order_ids.toString()
            });
        },
        checkoutConsumePacket: function() {
            var t = this, a = this;
            this.getVoiceSet();
            var i = wx.getStorageSync("yz_basic_info");
            if (i && i.globalParameter && 1 == i.globalParameter.consume_red_packet_status) {
                var s = e.getNetAddresss("plugin.consume-red-packet.Frontend.Modules.Packet.Controllers.has.index");
                e._postNetWork({
                    url: s,
                    data: {
                        order_id: this.data.order_ids.split(",")
                    },
                    success: function(e) {
                        1 == e.data.result ? (t.setData({
                            show1: !1,
                            showPacket: !0
                        }), a.triggerEvent("showOff", {
                            show1: t.data.show1
                        })) : t.toRedirect();
                    },
                    fail: function(e) {
                        t.toRedirect(), console.log(e);
                    }
                });
            } else this.toRedirect();
        },
        toRedirect: function() {
            wx.showToast({
                icon: "none",
                title: "支付成功",
                duration: 2e3
            }), this.data.order_sn && this.payCallBack();
        },
        getVoiceSet: function() {
            var t = e.getNetAddresss("plugin.store-cashier.frontend.cashier.voice.index");
            e._postNetWork({
                url: t,
                data: {
                    order_id: this.data.order_ids
                },
                success: function(t) {
                    var e = t.data;
                    if (1 == e.result) {
                        var a = wx.getBackgroundAudioManager();
                        a.title = "支付成功语音提示", a.src = e.data.url, a.play();
                    } else console.log(e);
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        PayShowbtn: function() {
            this.setData({
                agreementPayShow: !1
            });
        },
        _handleKeyPress: function(t) {
            var e = t.currentTarget.dataset.num;
            if (-1 == e) return !1;
            switch (String(e)) {
              case ".":
                this._handleDecimalPoint();
                break;

              case "D":
                this._handleDeleteKey();
                break;

              case "S":
                this._handleConfirmKey();
                break;

              default:
                this._handleNumberKey(e);
            }
        },
        _handleDecimalPoint: function() {
            if (this.data.payMuch.indexOf(".") > -1) return !1;
            this.data.payMuch.length ? this.setData({
                payMuch: this.data.payMuch + "."
            }) : this.setData({
                payMuch: "0."
            });
        },
        _handleDeleteKey: function() {
            var t = this.data.payMuch;
            t.length <= 1 ? this.setData({
                payMuch: "0"
            }) : this.setData({
                payMuch: t.substring(0, t.length - 1)
            });
        },
        _handleConfirmKey: function() {
            var t = this.data.payMuch;
            if (!t.length || 0 === Number(t)) return wx.showToast({
                icon: "none",
                title: "您目前未输入!",
                duration: 1500
            }), !1;
            t.indexOf(".") > -1 && t.indexOf(".") === t.length - 1 && (t = Number(t.substring(0, t.length - 1)).toFixed(2)), 
            t = Number(t).toFixed(2), this.setData({
                payMuch: t
            }), this.confirmOrder();
        },
        confirmOrder: function() {
            var t = this.data.payMuch;
            t && t > 0 ? (this.setData({
                payPrice: t,
                confirmOrderStatus: !0,
                isdisabled: !0
            }), this.preGoodsBuy()) : (wx.showToast({
                icon: "none",
                title: "请输入正确的付款金额!",
                duration: 1500
            }), this.setData({
                confirmOrderStatus: !1,
                isdisabled: !1
            }));
        },
        _handleNumberKey: function(t) {
            var e = this.data.payMuch;
            if (e.indexOf(".") > -1 && e.substring(e.indexOf(".") + 1).length < 2 && this.setData({
                payMuch: e + t
            }), !(e.indexOf(".") > -1)) if (0 == t && 0 == e.length) this.setData({
                payMuch: "0."
            }); else {
                if (e.length && 0 === Number(e.charAt(0))) return void this.setData({
                    payMuch: t
                });
                this.setData({
                    payMuch: e + t
                });
            }
        },
        preGoodsBuy: function() {
            var t = this, a = e.getNetAddresss("plugin.store-cashier.frontend.store.balance.goodsBuy.index");
            a += "&price=" + this.data.payPrice, a += "&store_id=" + this.data.store_id, a += "&member_coupon_ids=" + this.assembleCoupons(), 
            a += "&orders=" + JSON.stringify(this.assembleDeduction()), 0 == this.data.clicktag && (this.setData({
                clicktag: 1
            }), e._getNetWork({
                url: a,
                success: function(e) {
                    var a = e.data;
                    1 == a.result ? (a.data.balance && t.setData({
                        balanceNow: a.data.balance
                    }), t.setViewData(a.data), t.initCoupon(a.data.discount), t.initPayType()) : wx.showModal({
                        title: "提示",
                        content: a.msg,
                        showCancel: !1,
                        success: function(t) {
                            t.confirm && wx.navigateBack({
                                delta: 1
                            });
                        }
                    }), setTimeout(function() {
                        t.setData({
                            clicktag: 0
                        });
                    }, 1e3);
                },
                fail: function(t) {
                    console.log(t);
                }
            }));
        },
        assembleCoupons: function() {
            for (var t = [], e = 0; e < this.data.checkCouponList.length; e++) t.push(this.data.checkCouponList[e].id);
            return t;
        },
        assembleDeduction: function() {
            for (var t = [], e = 0; e < this.data.checkDeductionList.length; e++) t.push(this.data.checkDeductionList[e]);
            return t;
        },
        setViewData: function(t) {
            this.setData({
                allData: t,
                order_data: t.orders,
                loadDiscount: !0,
                goods_price: t.total_goods_price,
                dispatch_price: t.total_dispatch_price,
                deduction_price: t.total_deduction_price,
                price: t.total_price,
                service: t.orders[0].order_fees,
                datas: t.orders[0]
            }), "hotel" != this.data.cashier_type && this.setData({
                discount_price: t.total_discount_price
            });
        },
        initCoupon: function(t) {
            this.setData({
                isShowCoupon: "1" == t.whether_show_coupon
            });
            var e = t.member_coupons;
            null != e && "" != e && null != e && this.setData({
                coupon_size: e.length
            }), this.setData({
                coupons: e
            });
        },
        initPayType: function() {
            var t = this, a = e.getNetAddresss("plugin.store-cashier.frontend.store.balance.payment.getList");
            a += "&store_id=" + this.data.store_id, a += "&price=" + this.data.payMuch, e._getNetWork({
                url: a,
                success: function(e) {
                    var a = e.data;
                    if (1 == a.result) {
                        var i = a.data, s = {};
                        s.buttons = a.data, t.hanleIcon(s), t.setData({
                            payType: i,
                            typename: "支付",
                            show1: !0
                        }), t.triggerEvent("showOff", {
                            show1: t.data.show1
                        });
                    } else wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1500,
                        success: function() {
                            setTimeout(function() {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1500);
                        }
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        btnstyle: function(t) {
            return 1 == t || 6 == t || 9 == t || 12 == t || 20 == t || 22 == t ? "#64b42e" : 2 == t || 7 == t || 10 == t || 15 == t || 21 == t ? "#069ce7" : 3 == t ? "#fc992c" : 14 == t ? "#e36724" : 18 == t ? "#dedede" : "#fac337";
        },
        btnclass: function(t) {
            return 1 == t || 6 == t || 9 == t || 12 == t || 20 == t || 22 == t ? "icon-pay_b" : 2 == t || 7 == t || 10 == t || 15 == t || 21 == t ? "icon-pay_c" : 3 == t ? "icon-pay_d" : 14 == t ? "icon-pay_f" : 18 == t ? "card" : "icon-pay_a";
        },
        show1btn: function() {
            this.setData({
                show1: !1
            }), this.triggerEvent("showOff", {
                show1: this.data.show1
            });
        },
        discountHandle: function(t) {
            var e = t.detail, a = t.currentTarget.dataset.d.code, i = t.currentTarget.dataset.index;
            console.log(this.data.order_data[0], this.data.order_data[0].order_deductions[i]), 
            this.screenDiscount(this.data.order_data[0], {
                checked: e,
                code: a
            }), this.preGoodsBuy();
        },
        screenDiscount: function(t, e) {
            this.deductionListHandle(t, e);
        },
        deductionListHandle: function(t, e) {
            var a = this.data.checkDeductionList, i = !1;
            a.length > 0 && a.forEach(function(a, s) {
                a && a.pre_id == t.pre_id && (i = !0, e && e.checked ? a.deduction_ids && -1 == a.deduction_ids.indexOf(e.code) && a.deduction_ids.push(e.code) : e && !e.checked && a.deduction_ids && a.deduction_ids.indexOf(e.code) > -1 && a.deduction_ids.splice(a.deduction_ids.indexOf(e.code), 1));
            }), i || a.push({
                deduction_ids: e && e.code ? [ e.code ] : [],
                pre_id: t.pre_id
            }), this.setData({
                checkDeductionList: a
            });
        },
        showCoupon: function() {
            0 != this.data.coupon_size ? this.setData({
                popupCouponSpecs: !0
            }) : wx.showToast({
                icon: "none",
                title: "暂无优惠券使用",
                duration: 1500
            });
        },
        selectCoupon: function(e) {
            var a = e.detail, i = e.currentTarget.dataset.index;
            this.setData(t({}, "coupons[" + i + "].checked", a)), this.screenCoupon(a, this.data.coupons[i]), 
            this.setCouponView(), this.preGoodsBuy();
        },
        screenCoupon: function(t, e) {
            var a = e;
            if (t) if (this.data.checkCouponList.length > 0) {
                for (var i = 0; i < this.data.checkCouponList.length; i++) this.data.checkCouponList[i].id == a.id && this.data.checkCouponList.splice(i, 1);
                var s = this.data.checkCouponList;
                s.push(a), this.setData({
                    checkCouponList: s
                });
            } else {
                var o = this.data.checkCouponList;
                o.push(a), this.setData({
                    checkCouponList: o
                });
            } else if (this.data.checkCouponList.length > 0) for (var n = 0; n < this.data.checkCouponList.length; n++) if (this.data.checkCouponList[n].id == a.id) {
                var c = this.data.checkCouponList;
                c.splice(n, 1), this.setData({
                    checkCouponList: c
                });
            }
        },
        setCouponView: function() {
            this.setData({
                use_coupon_size: this.data.checkCouponList.length
            });
        },
        couponclose: function() {
            this.setData({
                popupCouponSpecs: !1
            });
        },
        realnameinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.realname": e
            });
        },
        mobileinp: function(t) {
            var e = t.detail.value;
            this.setData({
                "form.mobile": e
            });
        },
        agreementPayinp: function(t) {
            var e = t.detail;
            this.setData({
                agreementPay: e
            });
        },
        showPay: function() {
            this.setData({
                agreementPayShow: !0
            });
        },
        submit: function(t) {
            var a = t, i = this, s = e.getNetAddresss("plugin.store-cashier.frontend.store.balance.create.index");
            if (s += "&price=" + this.data.payMuch, s += "&store_id=" + this.data.store_id, 
            s += "&member_coupon_ids=" + JSON.stringify(this.assembleCoupons()), s += "&orders=" + JSON.stringify(this.assembleDeduction()), 
            0 == this.data.clicktag) {
                if (this.setData({
                    clicktag: 1
                }), this.data.agreementPayData && !this.data.agreementPay) return this.setData({
                    clicktag: 0
                }), void wx.showToast({
                    icon: "none",
                    title: "请先勾选支付协议",
                    duration: 1500
                });
                e._getNetWork({
                    url: s,
                    success: function(t) {
                        var e = t.data;
                        1 == e.result ? (i.setData({
                            order_ids: e.data.order_ids
                        }), i.getMultipleOrderInfo(a)) : (wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        }), i.data.isPayClick = !1), setTimeout(function() {
                            i.setData({
                                clicktag: 0
                            });
                        }, 1e3);
                    },
                    fail: function(t) {
                        console.log(t);
                    }
                });
            }
        },
        getMultipleOrderInfo: function(t) {
            var a = this, i = e.getNetAddresss("order.merge-pay");
            i += "&order_ids=" + this.data.order_ids, i += "&pid=" + this.data.uid, e._getNetWork({
                url: i,
                success: function(e) {
                    var i = e.data;
                    1 == i.result ? (a.setData({
                        order_sn: i.data.order_pay.pay_sn,
                        balance: i.data.member.credit2,
                        money: i.data.order_pay.amount,
                        uid: i.data.order_pay.uid,
                        order_pay_id: i.data.order_pay.id,
                        buttons: i.data.buttons,
                        min_redirect_url: i.data.min_redirect_url
                    }), a.handlePay(t, t.value)) : (wx.showModal({
                        title: "提示",
                        content: i.msg,
                        success: function(t) {
                            t.confirm ? (wx.reLaunch({
                                url: "/packageG/index/index"
                            }), console.log("用户点击确定")) : t.cancel && console.log("用户点击取消");
                        }
                    }), a.data.isPayClick = !1);
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        },
        getAgreement: function() {
            var t = this, a = e.getNetAddresss("plugin.store-cashier.frontend.cashier.center.getPaySet"), i = {
                store_id: this.data.store_id
            };
            e._getNetWork({
                url: a,
                data: i,
                success: function(e) {
                    var a = e.data;
                    1 == a.result ? t.setData({
                        agreementPayData: a.data.get_pay_protocol
                    }) : wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    }
});